﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Uow;

namespace $ext_safeprojectname$.usermodels
{

    /// <summary>
    /// 角色绑定权限
    ///</summary>
    [TypeFilter(typeof(RoleAttribute), Arguments = new object[] { "data", "add" })]
    public class GradeRoleAppService : $ext_safeprojectname$AppService
    {

        //private readonly IRepository<GradeRole, int> _repository;


        private readonly I$ext_safeprojectname$DbContext _dbContext;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="dbcontext"></param>
        public GradeRoleAppService(I$ext_safeprojectname$DbContext dbcontext
        )
        {

            _dbContext = dbcontext;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="page"></param>
        /// <param name="size"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<PagedResultDto<GradeRoleListDto>> GetListAsync(int page = 1, int size = 20)
        {

            var query = _dbContext.GradeRole.Where(t => 1 == 1)
                .OrderByDescending(xy => xy.Id);

            PagedResultDto<GradeRoleListDto> pagedResultDto = new PagedResultDto<GradeRoleListDto>();
            pagedResultDto.TotalCount = await query.CountAsync();
            var userList = await query.Page(page, size).ToListAsync();
            var temList = ObjectMapper.Map<List<GradeRole>, List<GradeRoleListDto>>(userList);
            pagedResultDto.Items = temList;
            return pagedResultDto;
        }

        /// <summary>
        /// 根据ID获取单项角色绑定权限
        ///</summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public GradeRoleDto GetByIdAsync(int id)
        {
            var query = _dbContext.GradeRole.Where(t => t.Id == id)
                .FirstOrDefault();
            var temList = ObjectMapper.Map<GradeRole, GradeRoleDto>(query);
            return temList;
        }

        /// <summary>
        /// 根据ID获取待更新单项信息角色绑定权限
        ///</summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public GradeRoleUpdateDto GetInfoForUpdateAsync(int id)
        {
            var query = _dbContext.GradeRole.Where(t => t.Id == id)
                .FirstOrDefault();
            var temList = ObjectMapper.Map<GradeRole, GradeRoleUpdateDto>(query);
            return temList;
        }


        /// <summary>
        /// 添加一个角色绑定权限
        ///</summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<GradeRoleDto> CreateItemAsync(GradeRoleAddDto input)
        {
            //这里需要排重

            var find = _dbContext.GradeRole.Where(x => x.GradeId == input.GradeId && x.RoleId == input.RoleId).Any();
            if (find)
            {
                throw new $ext_safeprojectname$Exception("数据重复，无法重复添加");
            }

            var newu = ObjectMapper.Map<GradeRoleAddDto, GradeRole>(input);
            //添加自定义
            _dbContext.Add(newu);
            await _dbContext.SaveChangesAsync();
            var backinfo = ObjectMapper.Map<GradeRole, GradeRoleDto>(newu);

            return backinfo;
        }
        /// <summary>
        /// 更新一个角色绑定权限
        ///</summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<GradeRoleDto> UpdateItemAsync(GradeRoleUpdateDto input)
        {
            var info = await _dbContext.GradeRole.Where(x => x.Id == input.Id).FirstOrDefaultAsync();
            if (info == null || info == default)
            {
                throw new $ext_safeprojectname$Exception("需要查询的信息不存在", "404");
            }

            var find = _dbContext.GradeRole.Where(x => x.GradeId == input.GradeId && x.RoleId == input.RoleId && x.Id!=input.Id).Any();
            if (find)
            {
                throw new $ext_safeprojectname$Exception("数据重复，无法重复添加");
            }

            ObjectMapper.Map<GradeRoleUpdateDto, GradeRole>(input, info);
            await _dbContext.SaveChangesAsync();
            var backinfo = ObjectMapper.Map<GradeRole, GradeRoleDto>(info);
            return backinfo;
        }


    }
}
